package definite.Guide.Book.CoreSwingComponent.JComponentClass;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class AncestorListenerSample {

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {

				JFrame frame = new JFrame("Listening Ancestor Event");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				AncestorListener ancestorListener = new AncestorListener() {

					@Override
					public void ancestorRemoved(AncestorEvent event) {
						System.out.println("Removed");
					}

					@Override
					public void ancestorMoved(AncestorEvent event) {
						System.out.println("Moved");
					}

					@Override
					public void ancestorAdded(AncestorEvent event) {
						System.out.println("Added");
					}
				};

				frame.getRootPane().addAncestorListener(ancestorListener);
				frame.setSize(300, 200);
				frame.setLocationRelativeTo(null);
				frame.getRootPane().setVisible(false);
				frame.getRootPane().setVisible(true);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
